//
//  YMDownContentView.swift
//  YMSwiftUI
//
//  Created by anscen on 2022/4/15.
//
//
//
import SwiftUI

struct YMDownContentView: View {
    @State public  var chapter :YMChapter
    @State private var model   :YMContent = YMContent()
    var body: some View {
        VStack {
            ScrollView {
                VStack {
                    Text(self.model.getContent).font(Font.system(size: 26)).foregroundColor(Color(UIColor(hex: "CFDFD7"))).padding(EdgeInsets(top: 20, leading: 20, bottom: 20, trailing: 20)).lineSpacing(5)
                }.padding(.zero)
            }.padding(.zero)
        }.onAppear {
            debugPrint(self.model.getContent)
            self.loadData()
        }.navigationTitle(self.model.title).navigationBarBackButtonHidden(true).navigationBarItems(leading: BaseNavigationBarBackButton())
    }
    func loadData(){
        if let content = self.chapter.content{
            self.model = YMContent()
            self.model.content = content
            self.model.title = self.chapter.title
            self.model.bookId = self.chapter.bookId
            debugPrint(self.model.getContent)
        }else{
            YMChapter.getContent(bookId: chapter.bookId, link: chapter.link) { json in
                self.model = json
            } failure: { error in
                
            }
        }
    }
}

